package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.info.FeedbackInfo;
import com.tracesystem.controller.info.LoginUserInfo;
import com.tracesystem.controller.form.FeedbackForm;
import com.tracesystem.dao.IssueBaseInfoDao;
import com.tracesystem.model.IssueBaseInfo;
import com.tracesystem.model.IssueComment;
import com.tracesystem.dao.IssueCommentDao;
import com.tracesystem.service.IssueCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.CommonsUtils;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.CommentFaildException;
import com.tracesystem.util.exception.code.IssueCommentExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  反馈业务实现
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
@Transactional
public class IssueCommentServiceImpl extends ServiceImpl<IssueCommentDao, IssueComment> implements IssueCommentService {
    @Autowired
    private IssueBaseInfoDao issueBaseInfoDao;
    /*
     * @Description:反馈业务实现方法
     * @Author: JMOCOK
     * @param feedbackForm: 反馈请求参数对象
     * @return: com.tracesystem.commons.Result 统一返回
     **/
    @Override
    public Result setFeedback(FeedbackForm feedbackForm) {
        IssueComment issueComment = WoniuCopyUtil.copyOne(feedbackForm, IssueComment.class);
        LoginUserInfo loginUserInfo = CommonsUtils.getLoginUserInfo();
        int insert = baseMapper.insert(issueComment);
        IssueBaseInfo issueBaseInfo = issueBaseInfoDao.selectById(feedbackForm.getIssueId());
        issueBaseInfo.setIssueState(feedbackForm.getLogState());
        if("已关闭".equals(feedbackForm.getLogState())){
            issueBaseInfo.setActEndTime(CommonsUtils.dateToLocalDate(new Date()));
        }
        int i = issueBaseInfoDao.updateById(issueBaseInfo);
        if(insert>0&&i>0){
            return Result.getInstance().setMsg("反馈成功");
        }
        throw new CommentFaildException(IssueCommentExceptionCode.ISSUE_COMMENT_FAILD_EXCEPTION);
    }

    /*
     * @Description:获取缺陷反馈集合
     * @Author: JMOCOK
     * @param issueId: 缺陷id
     * @return: com.tracesystem.commons.Result 统一返回
     **/
    @Override
    public Result<FeedbackInfo> loadFeedbackListByIssueId(Integer issueId) {
        QueryWrapper<IssueComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ISSUE_ID",issueId);
        List<IssueComment> issueComments = baseMapper.selectList(queryWrapper);
        List<FeedbackInfo> feedbackInfos = WoniuCopyUtil.copyMany(issueComments, FeedbackInfo.class);
        return Result.getInstanceList(FeedbackInfo.class).setMsg("查询成功").setData(feedbackInfos);
    }
}
